package com.ldcc.report_system.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ldcc.report_system.R;
import com.ldcc.report_system.ReportApplication;
import com.ldcc.report_system.api.results.DayReportResult;
import com.ldcc.report_system.api.results.WeekReportResult;
import com.ldcc.report_system.component.UpdateOrDeleteDialog;
import com.ldcc.report_system.ui.report.AddWeekReportActivity;
import com.ldcc.report_system.util.ActivitySkipUtil;
import com.ldcc.report_system.util.AlertUtil;
import com.ldcc.report_system.util.LoadingUtil;
import com.ldcc.report_system.util.ServerDateUtils;
import com.ldcc.report_system.util.http.ClientRequestManage;
import com.ldcc.report_system.util.http.HttpObserver;
import com.ldcc.report_system.util.http.ServerResponse;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author wwj[yeshengkele@outlool.com]
 * @description 周报列表适配器
 */
public class WeekListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * 加载提示框标识
     */
    public final static int FOOTER_ITEM = 1;

    /**
     * 普通item标识
     */
    public final static int ITEM = 0;

    // 数据
    private List<WeekReportResult> reportResultList;
    // 界面
    private Activity activity;
    // 是否有更多数据
    private boolean hasMore = false;

    private UpdateOrDeleteDialog updateOrDeleteDialog;

    public WeekListAdapter(List<WeekReportResult> reportResultList, Activity activity) {
        this.reportResultList = reportResultList;
        this.activity = activity;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == WeekListAdapter.FOOTER_ITEM) {
            // 设置加载提示框布局
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.common_list_loading_footer, parent, false);
            return new FooterHolder(view);
        }
        // 设置普通item布局
        view = View.inflate(activity, R.layout.fragment_report_list_item, null);
        return new ReportListItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        // 设置底部布局
        if (holder instanceof FooterHolder) {
            // 设置状态角标状态
            FooterHolder footerHolder = (FooterHolder) holder;
            Log.e("==============>", "onBindViewHolder: 设置状态" + this.hasMore);
            if (this.hasMore) {
                // 有更多数据显示正在加载
                footerHolder.setStatus(FooterHolder.LOADING);
            } else {
                // 没有数据换成没有更多数据
                footerHolder.setStatus(FooterHolder.THE_END);
            }
        }
        // 设置常规布局
        if (holder instanceof ReportListItemViewHolder) {
            ReportListItemViewHolder listItemViewHolder = (ReportListItemViewHolder) holder;
            // 绑定数据
            WeekReportResult weekReportResult = this.reportResultList.get(position);
            String reportResultId = weekReportResult.getId();
            // 数据时间
            String gmtCreate = weekReportResult.getGmtCreate();
            // 设置顶栏时间
            listItemViewHolder.getTopCreateDate().setText(ServerDateUtils.getDateString(gmtCreate, ServerDateUtils.MM_dd));
            //顶栏周数
            listItemViewHolder.getTopCreateWeek().setText("第" + ServerDateUtils.getDateToWeekOfMonth(gmtCreate) + "周");
            // 设置用户
            listItemViewHolder.getName().setText(weekReportResult.getCreateByName());
            // 日期
            listItemViewHolder.getGmtCreateDate().setText(ServerDateUtils.getDateString(gmtCreate, ServerDateUtils.MM_dd));
            //时间
            listItemViewHolder.getGmtCreateTime().setText(ServerDateUtils.getDateString(gmtCreate, ServerDateUtils.hh_mm));
            // 设置数据
            listItemViewHolder.getFinish().setText(weekReportResult.getFinish());
            listItemViewHolder.getReportType().setText("周报");
            // 内容点击事件
            listItemViewHolder.getReportBox().setOnClickListener(finishView -> {
                // 跳转展示页面
                ActivitySkipUtil.skipAnotherActivity(getActivity(), AddWeekReportActivity.class, reportResultId, false);
            });
            // 弹出修改或删除对话框事件
            listItemViewHolder.getTheMoreBtn().setOnClickListener(updateOrDeleteView -> {
                // 列表item事件
                this.updateOrDeleteDialog = new UpdateOrDeleteDialog(this.activity, R.style.dialogTheme, new UpdateOrDeleteDialog.OnClickListener() {
                    @Override
                    public void firstClick(Dialog dialog, String data) {
                        // 跳转修改页面
                        ActivitySkipUtil.skipAnotherActivity(getActivity(), AddWeekReportActivity.class, reportResultId, true);
                    }

                    @Override
                    public void secondClick(Dialog dialog, String data) {
                        // 完成删除操作
                        deleteWeekReport(reportResultId, position);
                        // 隐藏删除修改弹窗
                        updateOrDeleteDialog.dismiss();
                    }
                });
                // 显示
                this.updateOrDeleteDialog.show();
            });

        }

    }

    private void deleteWeekReport(String id, int position) {
        ClientRequestManage.getInstance().getWeekReportApi().remove(id).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<Object>(ReportApplication.getContext(), LoadingUtil.dialogLoadingOpen("请稍后", "正在连接服务器....", this.activity)) {

                    @Override
                    public void onSuccess(ServerResponse<Object> response_entity) {
                        AlertUtil.shortTime("删除成功");
                        reportResultList.remove(position);
                        // 通知 UI 更新
                        notifyDataSetChanged();
                    }
                });
    }

    @Override
    public int getItemCount() {
        return this.reportResultList == null ? 0 : this.reportResultList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        //如果position加1正好等于所有item的总和,说明是最后一个item,将它设置为脚布局
        //返回的count也要加1,因为添加了一个脚布局
        if (position + 1 == getItemCount()) {
            return WeekListAdapter.FOOTER_ITEM;
        } else {
            return WeekListAdapter.ITEM;
        }
    }

    /**
     * 更新数据方法
     *
     * @param list 数据
     */
    public void updateWeekReport(List<WeekReportResult> list) {
        if (list.size() == 0) {
            this.reportResultList.clear();
        } else {
            this.reportResultList = list;
        }
        // 通知UI更新
        this.notifyDataSetChanged();
    }

    public List<WeekReportResult> getReportResultList() {
        return reportResultList;
    }

    public void setReportResultList(List<WeekReportResult> reportResultList) {
        this.reportResultList = reportResultList;
    }

    public Activity getActivity() {
        return activity;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public boolean isHasMore() {
        return hasMore;
    }

    public void setHasMore(boolean hasMore) {
        this.hasMore = hasMore;
    }
}
